package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.common.R;
import com.reggie.dto.DishDto;
import com.reggie.entity.Dish;
import com.reggie.entity.DishFlavor;
import com.reggie.entity.Setmeal;
import com.reggie.entity.SetmealDish;
import com.reggie.mapper.DishMapper;
import com.reggie.service.DishFlavorService;
import com.reggie.service.DishService;
import com.reggie.service.SetmealDishService;
import com.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;

    /*
     * 新增菜品，同时插入菜品对应的口味数据：操作表dish  dishflavor
     * */
    @Transactional//多表操作，加入事务控制
    @Override
    public void saveWithDishFlavor(DishDto dishDto) {
        //保存菜品的基本信息到dish表
        this.save(dishDto);
        //菜品id
        Long dishId = dishDto.getId();
        //保存菜品口味数据到dishflavor表

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map(dishFlavor -> {
            dishFlavor.setDishId(dishId);
            return dishFlavor;
        }).collect(Collectors.toList());
        //dishDto.getFlavors()没有菜品id
        dishFlavorService.saveBatch(flavors);
    }

    /*
    * 根据id查询菜品信息和对应的口味信息
    * */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //菜品信息
        Dish dish = this.getById(id);

        DishDto dishDto=new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(flavors);
        return dishDto;
    }


    /*
    * 更新菜品信息，同时更新对应的口味信息
    * */
    @Transactional
    @Override
    public void updateWithDishFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //先清理flavor表中的对应的菜品的口味
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //插入新的数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //没有dishid这个字段，用流处理添加
        flavors = flavors.stream().map(dishFlavor -> {
            dishFlavor.setDishId(dishDto.getId());
            return dishFlavor;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);

    }

    /*
    * 删除菜品，同时如果套餐中有改菜品，将套餐停售
    * */
    @Override
    public R<String> batchDelete(List<Long> ids) {
        //根据ids查询看是否有套餐包含改菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getDishId,ids);
        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);
        if (setmealDishList!=null||setmealDishList.size()!=0){
            //表示改菜品出现在套餐中
            for (SetmealDish setmealDish : setmealDishList) {
                //根据setmealid查询到对应的setmeal（套餐）
                Long setmealId = setmealDish.getSetmealId();
                //把该套餐状态改为0
                LambdaUpdateWrapper<Setmeal> updateWrapper=new LambdaUpdateWrapper<>();
                updateWrapper.eq(Setmeal::getId,setmealId).set(Setmeal::getStatus,0);
                setmealService.update(updateWrapper);
            }
        }
        //没有，或者已经修改完成，删除改菜品
        LambdaQueryWrapper<Dish> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.in(Dish::getId,ids);
        this.remove(queryWrapper1);
        return R.success("删除菜品成功，对应套餐停售");
    }
}
